#include "menu_setting.h"
#include "menu_more_set.h"
#include "menu_frame.h"
#include "language.h"
#include <stdio.h>
#include <string.h>

static void OnLanguageFunction(const ST_KEY_INFO *pKeyInfo, const ST_MENU_ITEM_INFO *pItemInfo)
{
    int cmd;

    printf("--------------------------\n");
    printf("     %s%s\n", get_text(TEXT_LANGUAGE), get_text(TEXT_FUNCTION_TEST));
    printf("--------------------------\n");

    printf("%s(0-%s; 1-%s): ",
           get_text(TEXT_SELECT_OPTION),
           "中文",
           "English");
    scanf(" %d", &cmd); // 空格作用是忽略上次的回车

    if (cmd == 0)
    {
        set_language(LANGUAGE_CHINESE);
    }
    else
    {
        set_language(LANGUAGE_ENGLISH);
    }

    MMI_MenuExit(false); // 切换后自动退出
}

static void OnBluetoothFunction(const ST_KEY_INFO *pKeyInfo, const ST_MENU_ITEM_INFO *pItemInfo)
{
    int cmd;

    printf("--------------------------\n");
    printf("     %s%s\n", get_text(TEXT_BLUETOOTH), get_text(TEXT_FUNCTION_TEST));
    printf("--------------------------\n");

    printf("%s(0-%s): ",
           get_text(TEXT_SELECT_OPTION), get_text(TEXT_EXIT));
    scanf(" %d", &cmd); // 空格作用是忽略上次的回车

    if (cmd == 0)
    {
        MMI_MenuExit(false);
    }
}

static void OnBatteryFunction(const ST_KEY_INFO *pKeyInfo, const ST_MENU_ITEM_INFO *pItemInfo)
{
    int cmd;

    printf("--------------------------\n");
    printf("     %s%s\n", get_text(TEXT_BATTERY), get_text(TEXT_FUNCTION_TEST));
    printf("--------------------------\n");

    printf("%s(0-%s): ",
           get_text(TEXT_SELECT_OPTION), get_text(TEXT_EXIT));
    scanf(" %d", &cmd); // 空格作用是忽略上次的回车

    if (cmd == 0)
    {
        MMI_MenuExit(false);
    }
}

static void OnStorageFunction(const ST_KEY_INFO *pKeyInfo, const ST_MENU_ITEM_INFO *pItemInfo)
{
    int cmd;

    printf("--------------------------\n");
    printf("     %s%s\n", get_text(TEXT_STORE), get_text(TEXT_FUNCTION_TEST));
    printf("--------------------------\n");

    printf("%s(0-%s): ",
           get_text(TEXT_SELECT_OPTION), get_text(TEXT_EXIT));
    scanf(" %d", &cmd); // 空格作用是忽略上次的回车

    if (cmd == 0)
    {
        MMI_MenuExit(false);
    }
}

const ST_MENU_IMAGE gstImageSetting = {"$$$$$$$$$$", "%"};
/* 设置菜单 */
ST_MENU_LIST gastSetMenuTable[] = {
    MMI_MENU_ITEM_BIND(TEXT_LANGUAGE, NULL, NULL, NULL, OnLanguageFunction, NULL),
    MMI_MENU_ITEM_BIND(TEXT_BLUETOOTH, NULL, NULL, NULL, OnBluetoothFunction, NULL),
    MMI_MENU_ITEM_BIND(TEXT_BATTERY, NULL, NULL, NULL, OnBatteryFunction, NULL),
    MMI_MENU_ITEM_BIND(TEXT_STORE, NULL, NULL, NULL, OnStorageFunction, NULL),
    MMI_MENU_ITEM_BIND(TEXT_MORE, MENU_MoreSetEnter, MENU_MoreSetExit, MENU_MoreSetLoad, MENU_MoreSetTask, NULL),
};

/* 设置菜单显示效果 */
static void MENU_ShowSetting(ST_MENU_SHOW *ptShowInfo)
{
    uint8_t showNum = 3;
    uint8_t tmpselect;

    MMI_MenuLimitShowListNumber(ptShowInfo, &showNum);

    printf("\e[0;30;46m ------------- %s ------------- \e[0m\n", get_text(ptShowInfo->uMenuDesc.textId));

    for (int i = 0; i < showNum; i++)
    {
        tmpselect = i + ptShowInfo->showBaseItem;

        if (tmpselect == ptShowInfo->selectItem)
        {
            printf("\e[0;30;47m %d. %-16s\e[0m |\n", tmpselect + 1, get_text(ptShowInfo->uItemsListDesc[tmpselect].textId));
        }
        else
        {
            printf("\e[7;30;47m %d. %-16s\e[0m |\n", tmpselect + 1, get_text(ptShowInfo->uItemsListDesc[tmpselect].textId));
        }
    }
}

void MENU_SettingEnter(const ST_MENU_ITEM_INFO *pItemInfo)
{
    MMI_MenuBind(gastSetMenuTable, MMI_GET_MENU_NUM(gastSetMenuTable), MENU_ShowSetting);
}

void MENU_SettingLoad(const ST_MENU_ITEM_INFO *pItemInfo)
{
}

void MENU_SettingExit(const ST_MENU_ITEM_INFO *pItemInfo)
{
}

void MENU_SettingTask(const ST_KEY_INFO *pKeyInfo, const ST_MENU_ITEM_INFO *pItemInfo)
{
#if 0
    int cmd;

    printf("%s(0-%s; 1-%s; 2-%s; 3-%s; 4-%s): ",
           get_text(TEXT_SELECT_OPTION), get_text(TEXT_RETURN), get_text(TEXT_RETURN_MAIN_MENU),
           get_text(TEXT_ENTER), get_text(TEXT_NEXT), get_text(TEXT_PREVIOUS));
    scanf(" %d", &cmd); // 空格作用是忽略上次的回车

    switch (cmd)
    {
    case 0:
        MMI_MenuExit(false);
        break;
    case 1:
        MMI_MenuReset();
        break;
    case 2:
        MMI_MenuEnter();
        break;
    case 3:
        MMI_MenuSelectNext(true);
        break;
    case 4:
        MMI_MenuSelectPrev(true);
        break;

    default:
        break;
    }
#else
    if (!pKeyInfo)
        return;

    switch (pKeyInfo->code)
    {
    case EN_USER_KEY_SOFT4:
        MMI_MenuExit(false);
        break;
    case EN_USER_KEY_OK:
        MMI_MenuEnter();
        break;
    case EN_USER_KEY_RIGHT:
        MMI_MenuSelectNext(true);
        break;
    case EN_USER_KEY_LEFT:
        MMI_MenuSelectPrev(true);
        break;

    case EN_USER_KEY_8:
        MMI_MenuReset();
        break;

    default:
        break;
    }
    printf("%s(SOFT4-%s; 8-%s; OK-%s; RIGHT-%s; LEFT-%s):\n",
           get_text(TEXT_SELECT_OPTION), get_text(TEXT_RETURN), get_text(TEXT_RETURN_MAIN_MENU),
           get_text(TEXT_ENTER), get_text(TEXT_NEXT), get_text(TEXT_PREVIOUS));
#endif
}
